var World = function(drawer, size,stage){
    World.instance = this;
    this.drawer = drawer;
    this.personages = new Array();
    this.map = new Array();

    //TODO  in util class
    World.plants = new Array();
    World.maturity = new Array();
    World.healths = new Array();
    World.drawer = this.drawer;
    World.userPlants = new Array();

    World.getFor = function(array,key,value){
        for(var i in array){
            if (array[i][key] == value)
                return array[i];
        }
    }
    //
    this.worldCenter = null;
    this.oldWorldCenter  = null;// the world center of the last refresh
    this.worldInterface = new WorldInterface(this.drawer.engine.context,stage);


    this.worldInfo = new WorldInfo();
    this.worldInfo.size = size;
    this.worldInfo.drawDistanceFactor = 3;
    this.worldInfo.drawDistanceX = this.worldInfo.size.width * this.worldInfo.drawDistanceFactor;
    this.worldInfo.drawDistanceY = this.worldInfo.size.height * this.worldInfo.drawDistanceFactor;
    this.eventManager;
    this.networkManager;
}
World.prototype = {



    addPersonage : function(personage){
        var perso = new Personage(personage,false);
        this.drawer.drawObject(perso);
        this.personages.push(perso);
    },

    addPlantation : function(plantation){
        plantation.plantation = World.getFor(World.plants,"id",plantation.plantation);
        plantation.tile = World.getFor(this.map,"id",plantation.tile);
        plantation.maturity = World.getFor(World.maturity,"id",plantation.maturity);
        plantation.health = World.getFor(World.healths,"id",plantation.health);
        var plant = new UserPlantation(plantation,false);
        console.log(plant);
        this.drawer.drawObject(plant);
        World.userPlants.push(plant);
    },
    getPersonages : function(){
        return this.personages;
    },

    init : function(callback){
        this.worldCenter = { x : 0, y : 0 };
        this.oldWorldCenter = this.worldCenter;
        this.moveWorldCenter();
        this.loadPlants();
        this.loadMaturity();
        this.loadHealth();
        this.loadUserPlants();
        callback();
    },


    loadPlants : function(){
        var self = this;
        this.networkManager.getAllPlants(function(plants){
            console.log(plants);
            for(var i in plants){
                var p = new Plantation(plants[i]);
                World.plants.push(p);
            }
        });
    },


    loadUserPlants : function(){
        var self = this;
        this.networkManager.getAllUserPlants(function(plants){
            console.log(plants);
            for(var i in plants){
                self.addPlantation(plants[i]);
            }
        });
    },


    loadHealth : function(){
        var self = this;
        this.networkManager.getAllHealth(function(hs){
            for(var i in hs){
                var h = new Health(hs[i]);
                World.healths.push(h);
            }
        });
    },


    loadMaturity : function(){
        var self = this;
        this.networkManager.getMaturity(function(ms){
            for(var i in ms){
                var m = new Maturity(ms[i]);
                World.maturity.push(m);
            }
        });
    },



    getPersonageById : function(id){

        for(var i in this.personages){
            if (this.personages[i].id == id)
                return this.personages[i];
        }
        return null;
    },

    getPersonage : function(){
        return this.personages[0];
    },

    setMap : function (map){
        console.log("setMap");
        this.merge(map);
        this.removeUnseen();
        this.drawer.drawMap(this.map);
        this.oldWorldCenter = this.worldCenter;
    },

    merge : function (map){
        for (var i = 0; i < map.length; i++){
            var shouldAdd = true;
            for (var j = 0; j < this.map.length; j++){
                if (map[i].id == this.map[j].id)
                    shouldAdd = false;
            }
            if (shouldAdd){
                var t =new Tile(map[i],false);
                t.shouldShow = true;
                this.map.push(t);
            }
        }
    },

    removeUnseen : function(){
        for (var i = 0; i < this.map.length; i++){
            if (this.map[i].x - this.worldInfo.drawDistanceX > this.getPersonage().x ||
                this.map[i].x + this.worldInfo.drawDistanceX < this.getPersonage().x ||
                this.map[i].y - this.worldInfo.drawDistanceY > this.getPersonage().y ||
                this.map[i].y + this.worldInfo.drawDistanceY < this.getPersonage().y){
                    this.map[i].delete();
            }else{
                this.map[i].shouldShow = true;
            }
        }
    },

    getMap : function(){
        return this.map;
    },

    shiftWorldCenter : function(){
        this.drawer.engine.scene.translateBackground(
            {x:this.worldCenter.x,y:this.worldCenter.y},
            {x:this.getPersonage().x,y:this.getPersonage().y}
        );

        this.worldCenter = { x : this.getPersonage().x, y : this.getPersonage().y, z : 0 };
    },

    moveWorldCenter : function(){
        this.shiftWorldCenter();
    },
    nearBoundary : function(){
        if (this.oldWorldCenter.x + this.worldInfo.drawDistanceX < this.worldCenter.x + this.worldInfo.size.width * 1.5 ||
            this.oldWorldCenter.x - this.worldInfo.drawDistanceX > this.worldCenter.x - this.worldInfo.size.width * 1.5 ||
            this.oldWorldCenter.y + this.worldInfo.drawDistanceY < this.worldCenter.y + this.worldInfo.size.height * 1.5 ||
            this.oldWorldCenter.y - this.worldInfo.drawDistanceX > this.worldCenter.y + this.worldInfo.size.height * 1.5){
            return true;
        }
        return false;
    }



}
